package com.koron.css2.serviceManage.impl;


import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.AdminAreaBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
/**
 * 查询结构地址树下节点列表-水司
 * @author 甄玉晴
 *
 */
public class AdminAreaQueryList implements ServerInterface {
	private static Logger logger = LogManager.getLogger(AdminAreaQueryList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		 try {
	            PubAdminAreaMapper mapper = factory.getMapper(PubAdminAreaMapper.class);
	            AdminAreaBean bean = JsonUtils.objectToPojo(req.getData(), AdminAreaBean.class);
	            //先获取该节点的信息,并且判断该节点是否是服务区域的父节点
	            if(bean==null||StringUtils.isEmpty(bean.getId())) {
	            	bean=new AdminAreaBean();
	            	bean.setId("0");
	            }
				List<AdminAreaBean> list = mapper.queryListById(userInfo.getCurWaterCode(),bean.getId());
				if(list==null||list.size()==0) {//如果父节点是空的，则查询服务区域的子节点
					list=mapper.listGroup(bean);
				}
				info.setCode(Constant.MESSAGE_INT_SUCCESS);
				info.setDescription("success");
				info.setData(list);
	        } catch (Exception e) {
	            info.setCode(Constant.MESSAGE_INT_FAIL);
	            info.setDescription(e.getMessage());
	            logger.error(e.getMessage(), e);
	        }
	        return info;
	}
	

}
